using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Publicuse.Application;
using Geekeradmin.Authorize;
using Publicuse.Autofac;

using Publicuse.Interface;
using Publicuse.Util;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using Publicuse.Interface;
using Publicuse.Entity;
using Publicuse.Interface.Extend;

namespace Geekeradmin.Controllers.other
{
    /// <summary>
    /// 代码生成器模块
    /// </summary>
    [UserAuthorize("Admin"), AllowAnonymous]
    public class GeneratorController : BaseController
    {
        public GeneratorController(IAutofaceInstance autoface, IMySqlSugarService mySqlSugar, IWebHostEnvironment webHost, 
            IOptions<DBConnectionOption> options, IBaseServiceExt baseServiceExt)
            : base(autoface, mySqlSugar, webHost, options, baseServiceExt)
        {
        }

        /// <summary>
        /// 导出Excel模板上传
        /// </summary>
        /// <returns></returns>
        [HttpPost("UploadExportFile"), AllowAnonymous]
        public async Task<JsonResult> UploadExportFile()
        {
            string path = webHost.WebRootPath;
            //path = AppSettingUtil.UploadFilePath;
            var files = Request.Form.Files;
            var module = Request.Form["module"].ToString();
            if (module.Replace("`", "").Split('-').Length >= 2)
            {
                module = module.Replace("`", "").Split('-')[1];
            }
            else
            {
                var len = module.Replace("`", "").Split('/').Length;
                module = module.Replace("`", "").Split('/')[len - 1];
            }
            CommonResult result = await GeneratorApplication.GetApplication(container).UploadExportFile(files, path, module);
            return new JsonResult(result);
        }

        /// <summary>
        /// 导入Excel模板上传
        /// </summary>
        /// <returns></returns>
        [HttpPost("UploadImportFile"), AllowAnonymous]
        public async Task<JsonResult> UploadImportFile()
        {
            string path = webHost.WebRootPath;
            //path = AppSettingUtil.UploadFilePath;
            var files = Request.Form.Files;
            var module = Request.Form["module"].ToString();
            CommonResult result = await GeneratorApplication.GetApplication(container).UploadImportFile(files, path, module);
            return new JsonResult(result);
        }

        /// <summary>
        /// 重启服务
        /// </summary>
        /// <returns></returns>
        [HttpGet("Restart")]
        public async Task<JsonResult> Restart()
        {
            string path = webHost.WebRootPath;
            CommonResult result = await GeneratorApplication.GetApplication(container).Restart(path);
            return new JsonResult(result);
        }

        /// <summary>
        /// 根据页面模板设置生成具体代码
        /// </summary>
        /// <param name="generators"></param>
        /// <returns></returns>
        [HttpPost("Generatordetailed")]
        public async Task<JsonResult> Generatordetailed(List<Generator> generators)
        {
            string fileNameEntity = webHost.WebRootPath + "\\Generator\\Entity.txt";
            string fileNameExport = webHost.WebRootPath + "\\Generator\\Export.txt";
            string fileNameApplication = webHost.WebRootPath + "\\Generator\\Application.txt";
            string fileNameController = webHost.WebRootPath + "\\Generator\\Controller.txt";
            string fileNameModel = webHost.WebRootPath + "\\Generator\\Model.txt";
            string fileNameIndex = webHost.WebRootPath + "\\Generator\\Index.txt";
            string fileNameIndexAdd = webHost.WebRootPath + "\\Generator\\IndexAdd.txt";
            string fileNameScss = webHost.WebRootPath + "\\Generator\\Scss.txt";
            string fileNameHtml = webHost.WebRootPath + "\\Generator\\Html.txt";
            string fileNameHtmlAdd = webHost.WebRootPath + "\\Generator\\HtmlAdd.txt";
            string fileNameModule = webHost.WebRootPath + "\\Generator\\Module.txt";
            string fileNameRoute = webHost.WebRootPath + "\\Generator\\Route.txt";
            string fileNameShareModule = webHost.WebRootPath + "\\Generator\\ShareModule.txt";
            string exportPath = webHost.ContentRootPath;
            GeneratorTemplate template = new GeneratorTemplate()
            {
                fileNameEntity = fileNameEntity,
                fileNameExport = fileNameExport,
                fileNameApplication = fileNameApplication,
                fileNameController = fileNameController,
                fileNameModel = fileNameModel,
                fileNameIndex = fileNameIndex,
                fileNameIndexAdd = fileNameIndexAdd,
                fileNameScss = fileNameScss,
                fileNameHtml = fileNameHtml,
                fileNameHtmlAdd = fileNameHtmlAdd,
                fileNameModule = fileNameModule,
                fileNameRoute = fileNameRoute,
                fileNameShareModule = fileNameShareModule,
                exportPath = exportPath
            };
            CommonResult result = await GeneratorApplication.GetApplication(container).Generatordetailed(generators, template, 1);
            return new JsonResult(result);
        }

        /// <summary>
        /// 根据页面模板设置生成具体代码（ng13版本）
        /// </summary>
        /// <param name="generators"></param>
        /// <returns></returns>
        [HttpPost("Generatordetailednew")]
        public async Task<JsonResult> Generatordetailednew(List<Generator> generators)
        {
            string fileNameEntity = webHost.WebRootPath + "\\Generator\\Entity.txt";
            string fileNameExport = webHost.WebRootPath + "\\Generator\\Export.txt";
            string fileNameApplication = webHost.WebRootPath + "\\Generator\\Application.txt";
            string fileNameController = webHost.WebRootPath + "\\Generator\\Controller.txt";
            string fileNameModel = webHost.WebRootPath + "\\Generator\\Model.txt";
            string fileNameIndex = webHost.WebRootPath + "\\Generator\\Index.txt";
            string fileNameIndexAdd = webHost.WebRootPath + "\\Generator\\IndexAdd.txt";
            string fileNameScss = webHost.WebRootPath + "\\Generator\\Scss.txt";
            string fileNameHtml = webHost.WebRootPath + "\\Generator\\Html.txt";
            string fileNameHtmlAdd = webHost.WebRootPath + "\\Generator\\HtmlAdd.txt";
            string fileNameModule = webHost.WebRootPath + "\\Generator\\Module.txt";
            string fileNameRoute = webHost.WebRootPath + "\\Generator\\Route.txt";
            string fileNameShareModule = webHost.WebRootPath + "\\Generator\\ShareModule.txt";
            string exportPath = webHost.ContentRootPath;
            GeneratorTemplate template = new GeneratorTemplate()
            {
                fileNameEntity = fileNameEntity,
                fileNameExport = fileNameExport,
                fileNameApplication = fileNameApplication,
                fileNameController = fileNameController,
                fileNameModel = fileNameModel,
                fileNameIndex = fileNameIndex,
                fileNameIndexAdd = fileNameIndexAdd,
                fileNameScss = fileNameScss,
                fileNameHtml = fileNameHtml,
                fileNameHtmlAdd = fileNameHtmlAdd,
                fileNameModule = fileNameModule,
                fileNameRoute = fileNameRoute,
                fileNameShareModule = fileNameShareModule,
                exportPath = exportPath
            };
            CommonResult result = await GeneratorApplication.GetApplication(container).Generatordetailed(generators, template, 2);
            return new JsonResult(result);
        }
    }
}
